﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
* TEXTAREA extensions for better editing
*/

(function ()
{

    var fieldSelection = {

        replaceSelection: function ()
        {

            var e = this.jquery ? this[0] : this;
            var text = arguments[0] || '';

            return (

            /* mozilla / dom 3.0 */
        ('selectionStart' in e && function ()
        {
            var scrollTop = e.scrollTop;
            var caret = getCaret(e) + text.length;
            e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);

            if (e.selectionStart != null)
            {
                e.setSelectionRange(caret, caret);
                e.scrollTop = scrollTop;
            }
            return this;
        }) ||

            /* exploder */
        (document.selection && function ()
        {
            e.focus();
            document.selection.createRange().text = text;
            return this;
        }) ||

            /* browser not supported */
        function ()
        {
            e.value += text;
            return this;
        }
      )();
        }
    };
    jQuery.each(fieldSelection, function (i) { jQuery.fn[i] = this; });

})();
/* Extended for Document editing */
/* GetFocus to TextArea*/
$(document).ready(function ()
{
    $("FORM TEXTAREA.editor").keypress(function (e)
    {
        if (e != null && (e.keyCode == 13 || e.keyCode == 10) && e.ctrlKey)
        {
            LoadConverted(e, this.value);
        }
    });
    $("BUTTON[name=LoadConverted]").live('click', function (e)
    {
        e.preventDefault();
        var text = document.getElementById(EditedTextId).value;
        LoadConverted(e, text);
    });
});
/* convert TAB press into the 4 spaces insertion */
$(document).ready(function ()
{
    $("FORM TEXTAREA.editor").keydown(function (e)
    {
        if (e != null && e.keyCode == 9)
        {
            $(this).replaceSelection("    ", true);
            e.preventDefault();
        }
    });
});

var TextConvertedId = "";
asyncHandlersForEach.push(ConvertedTextScroll);
function ConvertedTextScroll()
{
    $("#" + TextConvertedId).each(function ()
    {
        this.scrollTop = $.cookie(TextConvertedId);
    });
}

var previewWindowForm;
var previewWindowFormInput;
function LoadConverted(evnt, text)
{
    if (previewWindowForm == null)
    {
        previewWindowForm = $("BUTTON[name=LoadConverted]").parents("FORM");
        previewWindowForm.attr("target", "previewWindow");
        previewWindowForm.attr("method", "POST");
        previewWindowForm.attr("action", ConvertTextUrl);

        previewWindowFormInput = $("<input type='hidden'>");
        previewWindowFormInput.attr("name", "SourceText");

        previewWindowForm.append(previewWindowFormInput);
    }
    previewWindowFormInput.val(text);
    previewWindowForm.submit();
}

function AdjustTextArea(obj)
{
    try
    {
        obj.focus();
        var caret = parseInt($.cookie(obj.id + "_caret"))
        if (obj.selectionStart != null)
        {
            obj.setSelectionRange(caret, caret);
        }
        else
        {
            var range = obj.createTextRange();
            range.collapse(true);
            range.moveEnd('character', caret);
            range.moveStart('character', caret);
            range.select();
        }
    } catch (ex) { }
}

/* Scroll (re)store position */
asyncHandlersForEach.push(ExtendTextAreaEditing);
function ExtendTextAreaEditing()
{
    $("PRE.editor[id]").each(function (e)
    {  // restore scrolls on page load
        this.scrollTop = $.cookie($(this).attr("id"));
    });
    $("PRE.editor[id]").scroll(function (e)
    { // add scroll position 'onScroll' PRE
        $.cookie($(this).attr("id"), this.scrollTop, { expires: 7, path: "/" });
    });
    $("TEXTAREA.editor[id]").scroll(function (e)
    { // add scroll position 'onScroll' TEXTAREA
        $.cookie($(this).attr("id"), this.scrollTop, { expires: 7, path: "/" });
    });
    $("TEXTAREA.editor[id]").keyup(function (e)
    { // remember the cursor
        $.cookie($(this).attr("id") + "_caret", getCaret(this), { expires: 7, path: "/" });
    });
    $("TEXTAREA.editor[id]").keyup(function (e)
    { // remember the cursor
        $.cookie($(this).attr("id") + "_caret", getCaret(this), { expires: 7, path: "/" });
    });
    $("TEXTAREA.editor[id]").live('click', function (e)
    { // remember the cursor
        $.cookie($(this).attr("id") + "_caret", getCaret(this), { expires: 7, path: "/" });
    });
    $("TEXTAREA.editor[id]").each(function (e)
    {
        AdjustTextArea(this);
        this.scrollTop = $.cookie(this.id);
    });
}

/* The way how to get the position of the Caret */
function getCaret(el)
{
    if (el.selectionStart != null)
    { // FF
        return el.selectionStart;
    }
    if (document.selection)
    { // IE
        el.focus();
        var r = document.selection.createRange();
        if (r == null)
        {
            return 0;
        }

        var re = el.createTextRange(), rc = re.duplicate();
        re.moveToBookmark(r.getBookmark());
        rc.setEndPoint('EndToStart', re);

        var caret = rc.text.length;
        var lineBreaks = rc.text.split('\n').length - 1; // trick for the line breaks
        return caret - lineBreaks;
    }
    return 0;
}